﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContactForm.ascx.cs" company="Tobias Lekman">
//   Licensed under Microsoft Public License (Ms-PL). See lekman.codeplex.com/license for more information.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace LekmanCom.Controls
{
    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Web.UI;

    using LekmanCom.Properties;

    /// <summary>
    /// The contact form.
    /// </summary>
    public partial class ContactForm : UserControl
    {
        #region Methods

        /// <summary>
        /// Called when the contact form submit button has been clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        protected void OnSubmitContactFormClick(object sender, EventArgs e)
        {
            SendMessage(this.Name.Text, this.Email.Text, this.Message.Text);
            this.FormPanel.Visible = false;
            this.ThankYouPanel.Visible = true;
        }

        /// <summary>
        /// Sends the message as an email to myself.
        /// </summary>
        /// <param name="name">
        /// The name of the sender.
        /// </param>
        /// <param name="email">
        /// The email address.
        /// </param>
        /// <param name="message">
        /// The message to send.
        /// </param>
        private static void SendMessage(string name, string email, string message)
        {
            message = String.Format("Name: {0}\r\nEmail: {1}\r\n\r\n\r\n{2}", name, email, message);
            var mail = new MailMessage(Resources.MyEmail, Resources.MyEmail, "Lekman.com Web Contact Form", message);
            var client = new SmtpClient("smtp.live.com", 587)
                {
                    UseDefaultCredentials = false, 
                    EnableSsl = true, 
                    Credentials = new NetworkCredential(Resources.MyEmail, Resources.MyLivePassword)
                };
            client.Send(mail);
        }

        #endregion
    }
}